<?php

namespace App\Http\Controllers\Back;

use App\Util\Result;
use App\Model\Picture;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

/**
 * 上传处理功能
 * Class UploadController
 * @package App\Http\Controllers\Admin
 */
class UploadController extends Controller
{
    /**
     * 上传图片
     * @author gaoyoulong
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function img(Request $request){
        if(!$request->hasFile('file')){
            $noFile = Result::failure('文件不存在');
            return response()->json($noFile->getResult());
        }

        $file = $request->file('file');
        $valid = Picture::valid($file);
        if(!$valid->isSuccess()){
            return response()->json($valid->getResult());
        }

        $res = Picture::toLocal($file,'temp');

        return response()->json($res->getResult());
    }

    /**
     * 上传文件
     * @author gaoyoulong
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function file(Request $request){
        if(!$request->hasFile('file')){
            $noFile = Result::failure('文件不存在');
            return response()->json($noFile->getResult());
        }

        $file = $request->file('file');
        $res = Picture::toLocal2($file,'else');

        return response()->json($res->getResult());
    }
}
